import 'package:flutter/material.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';

import 'constants.dart';

abstract class BasePage extends StatefulWidget {
  late BasePageState basePageState;

  BasePage({super.key});

  @override
  BasePageState createState() {
    basePageState = getState();
    return basePageState;
  }

  BasePageState getState();
}

abstract class BasePageState<T extends BasePage> extends State<T> {
  bool _isAppBarShow = true; //导航栏是否显示

  bool _isShowError = false; //错误信息是否显示
  String _errorContentMessage = "网络请求失败，请检查您的网络";
  String _errImgPath = "assets/images/icon_error.png";

  bool _isLoading = false;

  bool _isEmpty = false;
  String _emptyWidgetContent = "暂无数据~";
  String _emptyImgPath = "assets/images/icon_empty.png";

  final FontWeight _fontWidget = FontWeight.w600; //错误页面和空页面的字体粗度

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: _getBaseAppBar(),
        body: Container(
          color: Colors.white, //背景颜色，可自己变更
          child: Stack(
            children: <Widget>[
              getContentWidget(context),
              _getBaseErrorWidget(),
              _getBaseEmptyWidget(),
              _getBassLoadingWidget(),
            ],
          ),
        ));
  }

  @override
  void dispose() {
    super.dispose();
  }

  Widget getContentWidget(BuildContext context);

  Widget getErrorWidget() {
    return Container(
      padding: const EdgeInsets.fromLTRB(0, 0, 0, 80),
      color: Colors.white,
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image(
              image: AssetImage(_errImgPath),
              width: 120,
              height: 120,
            ),
            Container(
              margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: Text(_errorContentMessage,
                  style: TextStyle(
                    color: Colors.grey,
                    fontWeight: _fontWidget,
                  )),
            ),
            Container(
                margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                child: OutlinedButton(
                  child: Text("重新加载",
                      style: TextStyle(
                        color: Colors.grey,
                        fontWeight: _fontWidget,
                      )),
                  onPressed: () => { onClickErrorHandler() },
                ))
          ],
        ),
      ),
    );
  }

  Widget getLoadingWidget() {
    return Center( child: Center(
        child: LoadingAnimationWidget.progressiveDots(
        color: Constants.themeColor,
        size: 100,
    )));
  }

  Widget getEmptyWidget() {
    return Container(
      padding: const EdgeInsets.fromLTRB(0, 0, 0, 100),
      color: Colors.white,
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Container(
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Image(
                color: Colors.black12,
                image: AssetImage(_emptyImgPath),
                width: 150,
                height: 150,
              ),
              Container(
                margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
                child: Text(_emptyWidgetContent,
                    style: TextStyle(
                      color: Colors.grey,
                      fontWeight: _fontWidget,
                    )),
              )
            ],
          ),
        ),
      ),
    );
  }

  PreferredSizeWidget _getBaseAppBar() {
    return PreferredSize(
        preferredSize: const Size.fromHeight(50),
        child: Offstage(
          offstage: !_isAppBarShow,
          child: getAppBar(),
        ));
  }

  ///导航栏 appBar
  AppBar getAppBar();

  Widget _getBaseErrorWidget() {
    return Offstage(
      offstage: !_isShowError,
      child: getErrorWidget(),
    );
  }

  Widget _getBassLoadingWidget() {
    return Offstage(
      offstage: !_isLoading,
      child: getLoadingWidget(),
    );
  }

  Widget _getBaseEmptyWidget() {
    return Offstage(
      offstage: !_isEmpty,
      child: getEmptyWidget(),
    );
  }

  ///点击错误页面
  void onClickErrorHandler();

  ///设置错误提示信息
  void setErrorContent(String content) {
    setState(() {
      _errorContentMessage = content;
    });
  }

  ///设置导航栏隐藏或者显示
  void setAppBarVisible(bool isVisible) {
    setState(() {
      _isAppBarShow = isVisible;
    });
  }

  void showContent() {
    setState(() {
      _isEmpty = false;
      _isLoading = false;
      _isShowError = false;
    });
  }

  void showLoading() {
    setState(() {
      _isEmpty = false;
      _isLoading = true;
      _isShowError = false;
    });
  }

  void showEmpty() {
    setState(() {
      _isEmpty = true;
      _isLoading = false;
      _isShowError = false;
    });
  }

  void showError() {
    setState(() {
      _isEmpty = false;
      _isLoading = false;
      _isShowError = true;
    });
  }

  ///设置空页面内容
  void setEmptyWidgetContent(String content) {
    setState(() {
      _emptyWidgetContent = content;
    });
  }

  ///设置错误页面图片
  void setErrorImage(String imagePath) {
    setState(() {
      _errImgPath = imagePath;
    });
  }

  ///设置空页面图片
  void setEmptyImage(String imagePath) {
    setState(() {
      _emptyImgPath = imagePath;
    });
  }
}
